package com.example.jdai.jdai.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.example.jdai.jdai.R;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.threeparty.WheelView;

import java.util.Arrays;

import butterknife.ButterKnife;

/**
 * Created by hellopyj on 2017/1/9.
 */

public class MyConnectPopuWindows extends PopupWindow {
    WheelView wvConnect;
    Button btCancle;
    Button btOk;
    int selectItem=0;
    String itemName="";
    MySetOk  mySetOk;
    Context context;
    int MYOF=2;
    //private static final String[] PLANETS = new String[]{"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Uranus", "Neptune", "Pluto"};

    public MyConnectPopuWindows(Context context,String[] toShow,String name) {
        super(context);
        this.context=context;
        int i = 0;
        View contentView = LayoutInflater.from(context).inflate(R.layout.my_connect_popupwindow, null);
        ButterKnife.bind(contentView);
        if(""!=name) {
            for (; i < toShow.length; i++) {
                if (name.equals(toShow[i]))
                    break;
            }
            if(i>=toShow.length){
                i=0;
            }
        }
        if(Config.ISDEBUG)
        Log.v("i",i+"");
            wvConnect = (WheelView) contentView.findViewById(R.id.wv_connect);
            btCancle = (Button) contentView.findViewById(R.id.bt_cancle);
            btOk = (Button) contentView.findViewById(R.id.bt_ok);
            wvConnect.setOffset(MYOF);
            wvConnect.setItems(Arrays.asList(toShow));
            itemName = toShow[0];
            itemName = toShow[i];
            wvConnect.setSeletion(i);
            wvConnect.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
                @Override
                public void onSelected(int selectedIndex, String item) {
                    selectItem = selectedIndex-MYOF;
                    itemName = item;
                    if (Config.ISDEBUG)
                    Log.d("sss", "selectedIndex: " + selectedIndex + ", item: " + item);
                }
            });
            btCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
            setContentView(contentView);
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setAnimationStyle(R.style.MenuAnimationFade);
            setFocusable(true);
            setOutsideTouchable(true);
            setBackgroundDrawable(new BitmapDrawable());
            btOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mySetOk != null)
                        mySetOk.onOkclick(selectItem, itemName);
                    dismiss();
                }
            });
            // WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
            // lp.alpha = (float) 0.8; //0.0-1.0
            // ((Activity)context).getWindow().setAttributes(lp);

        }
    public MyConnectPopuWindows setBtOk(MySetOk mySetOk)
    {
        this.mySetOk=mySetOk;
        return this;
    }

    public interface MySetOk{
        void onOkclick(int index,String itemname);

    }

    @Override
    public void dismiss() {
        super.dismiss();
       // WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
       // lp.alpha = (float) 1; //0.0-1.0
       // ((Activity)context).getWindow().setAttributes(lp);
    }
}
